package com.vinaprime.primeschedule.businesslayer;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.annotation.SuppressLint;
import android.content.Context;
import android.database.Cursor;

import com.vinaprime.primeschedule.databaselayer.VPProject_DL;
import com.vinaprime.primeschedule.entities.VPProject;
@SuppressLint("NewApi")
public class VPProject_BL {
	private VPProject_DL myProjectDL;
	private DateFormat myDbDateFormat, myDateFormat;
	
	public VPProject_BL(Context context, String path){
		this.myProjectDL = new VPProject_DL(context, path);
		
		this.myDbDateFormat = new SimpleDateFormat(this.myProjectDL.getDbDateformat());
		this.myDateFormat = DateFormat.getDateInstance();
	}
	
	public void addProject(VPProject project)
	{
		this.myProjectDL.addProject(project);
	}
	
	public void deleteProject(int projectId)
	{
		this.myProjectDL.deleteProject(projectId);
	}
	
	public String getDbTableColRowId() {
		return this.myProjectDL.getDbTableColRowId();
	}
	
	public String getDbTableColId() {
		return this.myProjectDL.getDbTableColId();
	}

	public String getDbTableColCode() {
		return this.myProjectDL.getDbTableColCode();
	}

	public String getDbTableColName() {
		return this.myProjectDL.getDbTableColName();
	}

	public String getDbTableColDescription() {
		return this.myProjectDL.getDbTableColDescription();
	}

	public String getDbTableColEnableFlag() {
		return this.myProjectDL.getDbTableColEnableFlag();
	}

	public String getDbTableColStartDate() {
		return this.myProjectDL.getDbTableColStartDate();
	}

	public String getDbTableColEndDate() {
		return this.myProjectDL.getDbTableColEndDate();
	}
	
	public String getDbDateformat()
	{
		return this.myProjectDL.getDbDateformat();
	}
	
	public int getNewProjectId()
	{
		int result = 0;
		Cursor cur = this.myProjectDL.getNewProjectId();
		
		if(cur.moveToFirst())
		{
			if(!cur.isNull(cur.getColumnIndexOrThrow(VPProject_DL.getDbTableColId())))
			{
				result = Integer.parseInt(cur.getString(cur.getColumnIndexOrThrow(VPProject_DL.getDbTableColId())));
			}
		}
		
		return result + 1;
	}
	
	public VPProject getProject(int id) throws IllegalArgumentException, ParseException{
		VPProject myProject = new VPProject();
		
		Date fromDate, toDate;
		
		Cursor cur = this.myProjectDL.getProject(id);
		
		if(cur.moveToFirst())
		{
			cur.moveToFirst();
			myProject.setVppId(Integer.parseInt(cur.getString(cur.getColumnIndexOrThrow(VPProject_DL.getDbTableColRowId()))));
			myProject.setVppCode(cur.getString(cur.getColumnIndexOrThrow(VPProject_DL.getDbTableColCode())));
			myProject.setVppName(cur.getString(cur.getColumnIndexOrThrow(VPProject_DL.getDbTableColName())));
			myProject.setVppDescription(cur.getString(cur.getColumnIndexOrThrow(VPProject_DL.getDbTableColDescription())));
			myProject.setVppEnableFlag(cur.getString(cur.getColumnIndexOrThrow(VPProject_DL.getDbTableColEnableFlag())));
			
			if(!cur.isNull(cur.getColumnIndexOrThrow(VPProject_DL.getDbTableColStartDate())))
			{
				fromDate = this.myDbDateFormat.parse(cur.getString(cur.getColumnIndexOrThrow(VPProject_DL.getDbTableColStartDate())));
				myProject.setVppStartDate(this.myDateFormat.parse(this.myDateFormat.format(fromDate)));
			}
			
			if(!cur.isNull(cur.getColumnIndexOrThrow(VPProject_DL.getDbTableColEndDate())))
			{
				toDate = this.myDbDateFormat.parse(cur.getString(cur.getColumnIndexOrThrow(VPProject_DL.getDbTableColEndDate())));
				myProject.setVppEndDate(this.myDateFormat.parse(this.myDateFormat.format(toDate)));
			}
		}
		
		return myProject;
	}
	
	public List<VPProject> getAllProject() throws IllegalArgumentException, ParseException{
		List<VPProject> myListProject = new ArrayList<VPProject>();
		VPProject myProject = new VPProject();
		Date fromDate, toDate;
		
		Cursor cur = this.myProjectDL.getAllProject();
				
		if (cur.moveToFirst()) {
            do {

            	myProject = new VPProject(); 

            	myProject.setVppId(Integer.parseInt(cur.getString(cur.getColumnIndexOrThrow(VPProject_DL.getDbTableColRowId()))));
    			myProject.setVppCode(cur.getString(cur.getColumnIndexOrThrow(VPProject_DL.getDbTableColCode())));
    			myProject.setVppName(cur.getString(cur.getColumnIndexOrThrow(VPProject_DL.getDbTableColName())));
    			myProject.setVppDescription(cur.getString(cur.getColumnIndexOrThrow(VPProject_DL.getDbTableColDescription())));
    			myProject.setVppEnableFlag(cur.getString(cur.getColumnIndexOrThrow(VPProject_DL.getDbTableColEnableFlag())));

    			if(!cur.isNull(cur.getColumnIndexOrThrow(VPProject_DL.getDbTableColStartDate())))
    			{
    				fromDate = this.myDbDateFormat.parse(cur.getString(cur.getColumnIndexOrThrow(VPProject_DL.getDbTableColStartDate())));
    				
    				myProject.setVppStartDate(this.myDateFormat.parse(this.myDateFormat.format(fromDate)));
    			}
    			
    			if(!cur.isNull(cur.getColumnIndexOrThrow(VPProject_DL.getDbTableColEndDate())))
    			{
    				toDate = this.myDbDateFormat.parse(cur.getString(cur.getColumnIndexOrThrow(VPProject_DL.getDbTableColEndDate())));
    				
    				myProject.setVppEndDate(this.myDateFormat.parse(this.myDateFormat.format(toDate)));
    			}
    			
                myListProject.add(myProject);
            } while (cur.moveToNext());
        }
		
		return myListProject;
	}
	
	public Cursor getAllProjectToCursor() throws IllegalArgumentException, ParseException{
		Cursor cur = this.myProjectDL.getAllProject();
		if(cur != null)
		{
			cur.moveToFirst();
		}
		
		return cur;
	}
	
	public List<VPProject> getProject(String keyWord) throws IllegalArgumentException, ParseException
	{
		List<VPProject> myProjectList = new ArrayList<VPProject>();
		
		VPProject myProject = new VPProject();
		Date fromDate, toDate;
		
		Cursor cur = this.myProjectDL.getProject(keyWord);
				
		if (cur.moveToFirst()) {
            do {

            	myProject = new VPProject(); 

            	myProject.setVppId(Integer.parseInt(cur.getString(cur.getColumnIndexOrThrow(VPProject_DL.getDbTableColRowId()))));
    			myProject.setVppCode(cur.getString(cur.getColumnIndexOrThrow(VPProject_DL.getDbTableColCode())));
    			myProject.setVppName(cur.getString(cur.getColumnIndexOrThrow(VPProject_DL.getDbTableColName())));
    			myProject.setVppDescription(cur.getString(cur.getColumnIndexOrThrow(VPProject_DL.getDbTableColDescription())));
    			myProject.setVppEnableFlag(cur.getString(cur.getColumnIndexOrThrow(VPProject_DL.getDbTableColEnableFlag())));

    			if(!cur.isNull(cur.getColumnIndexOrThrow(VPProject_DL.getDbTableColStartDate())))
    			{
    				fromDate = this.myDbDateFormat.parse(cur.getString(cur.getColumnIndexOrThrow(VPProject_DL.getDbTableColStartDate())));
    				
    				myProject.setVppStartDate(this.myDateFormat.parse(this.myDateFormat.format(fromDate)));
    			}
    			
    			if(!cur.isNull(cur.getColumnIndexOrThrow(VPProject_DL.getDbTableColEndDate())))
    			{
    				toDate = this.myDbDateFormat.parse(cur.getString(cur.getColumnIndexOrThrow(VPProject_DL.getDbTableColEndDate())));
    				
    				myProject.setVppEndDate(this.myDateFormat.parse(this.myDateFormat.format(toDate)));
    			}
    			
    			myProjectList.add(myProject);
            } while (cur.moveToNext());
        }
		
		return myProjectList;
	}
	
	public Cursor getProjectToCursor(String keyWord)
	{
		Cursor cur = this.myProjectDL.getProject(keyWord);
		if(cur != null)
		{
			cur.moveToFirst();
		}
		
		return cur;
	}
	
	public void updateProject(VPProject project){
		this.myProjectDL.updateProject(project);
	}
}
